#include "Simon.h"

#define SPEED_X 0.3f
#define SPEED_Y 0.4f
#define MAX_HEIGHT 60.0f

Simon::Simon(void): DynamicObject()
{
}

Simon::Simon(int _posX, int _posY): DynamicObject(_posX, _posY, 0, -SPEED_Y, EnumID::Simon_ID)
{
	_action = Action::Idle;
	_vLast = 0.2f;
	_hasJump = false;
	_heightJump = 0.0f;
	jump = new CSprite(Singleton::getInstance()->getTexture(EnumID::Simon_ID), 4, 4, 300);
}

void Simon::Update(int deltaTime)
{
	switch(_action)
	{
	case Action::Run_Left:
		sprite->Update(deltaTime);
		break;
	case Action::Run_Right:
		sprite->Update(deltaTime);
		break;
	}		
	posX += vX *deltaTime;	
	posY += vY *deltaTime;

	if(_hasJump)
	{
		_heightJump += vY * deltaTime;
		if( _heightJump >= MAX_HEIGHT)
		{			
			vY = -SPEED_Y;
		}
	}
}

void Simon::Collision(list<GameObject*> obj, float dt) 
{
	list<GameObject*>::iterator _itBegin;
	for (_itBegin = obj.begin(); _itBegin != obj.end(); _itBegin++)
	{
		float moveX;
		float moveY;
		float normalx;
		float normaly;
		GameObject* other = (*_itBegin);

		Box boxSimon = this->GetBox();
		boxSimon.w -= 14;
		boxSimon.y += 3;
		Box boxOther = other->GetBox();


		if(AABB(boxSimon, boxOther, moveX, moveY) == true)
		{
			switch(other->id)
			{
			case EnumID::Brick_ID:	
				float normalx, normaly;
				if(vY < 0)
				{
					_hasJump = false;
					posY += moveY;
				}
				break;
			}
		}
		else if(AABB(boxSimon, boxOther, moveX, moveY) == false)
		{
			if(other->canMove == true)
			{
				boxSimon.vx -= boxOther.vx;
				boxSimon.vy -= boxOther.vy;
				boxOther.vx = 0;
				boxOther.vy = 0;
			}

			Box broadphasebox = GetSweptBroadphaseBox(boxSimon);
			if (AABBCheck(broadphasebox, boxOther) == true)
			{
				float normalx, normaly;
				float collisiontime = SweptAABB(boxSimon, boxOther, normalx, normaly);
				boxSimon.x += boxSimon.vx * collisiontime;
				boxSimon.y += boxSimon.vy * collisiontime;
				float remainingtime = 1.0f - collisiontime;
				if (collisiontime < 1.0f)
				{
					// perform response here
					switch(other->id)
					{
					case EnumID::BlackLeopard_ID:
						other->posY = 300;
						break;
					}
				}
				else
					if(AABBCheck(boxSimon, boxOther) == true)
					{
					}
			}
		}
	}
}

void Simon::Draw(CCamera* camera)
{
	D3DXVECTOR2 center = camera->Transform(posX, posY);

	if(vX > 0)
	{
		if(_hasJump && _heightJump >= MAX_HEIGHT / 2)
		{
			jump->DrawFlipX(center.x, center.y);
			return;
		}
		switch(_action)
		{
		default:
			sprite->DrawFlipX(center.x, center.y);
			break;
		}
	}
	else if(vX < 0)
	{	
		if(_hasJump && _heightJump >= MAX_HEIGHT / 2)
		{
			jump->Draw(center.x, center.y);
			return;
		}	
		switch(_action)
		{
		default:
			sprite->Draw(center.x, center.y);
			break;
		}
	}
	else
	{
		if(_vLast > 0)
		{
			if(_hasJump && _heightJump >= MAX_HEIGHT / 2)
			{
				jump->DrawFlipX(center.x, center.y);
				return;
			}
			sprite->DrawFlipX(center.x, center.y);
		}
		else 
		{
			if(_hasJump && _heightJump >= MAX_HEIGHT / 2)
			{
				jump->Draw(center.x, center.y);
				return;
			}			
			sprite->Draw(center.x, center.y);
		}
	}
}

void Simon::TurnLeft()
{
	if(_hasJump)
		return;
	vX = -SPEED_X;
	_action = Action::Run_Left;
}

void Simon::TurnRight()
{
	if(_hasJump)
		return;
	vX = SPEED_X;
	_action = Action::Run_Right;
}

void Simon::Jump()
{
	if(!_hasJump)
	{
		vY = SPEED_Y;
		_heightJump = 0;
		sprite->SelectIndex(0);
		_action = Action::Jump;
		_hasJump = true;
	}
}

void Simon::Stop()
{
	if(_action == Action::Idle)
		return;
	if(vX != 0)
		_vLast = vX;
	vX = 0;
	_action = Action::Idle;
	sprite->SelectIndex(0);
}

Simon::~Simon(void)
{
}
